package si.dejan.picflip.views
{
	import com.greensock.TweenLite;
	
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	
	import mx.core.Application;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import si.dejan.picflip.ApplicationFacade;
	import si.dejan.picflip.model.SearchProxy;
	
	import spark.skins.spark.ApplicationSkin;
	
	public class SearchBoxMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "SearchBoxMediator";
		
		private var searchProxy:SearchProxy;
		
		public function SearchBoxMediator(viewComponent:SearchBox)
		{
			super(NAME, viewComponent);
			
			searchProxy = facade.retrieveProxy(SearchProxy.NAME) as SearchProxy;
		}
		
		override public function onRegister():void
		{
			ui.addEventListener(ui.SEARCH, handleSearch);
			ui.addEventListener(ui.GET_NEXT, handleGetNext);
			ui.addEventListener(ui.GET_PREV, handleGetPrev);
			ui.addEventListener(ui.EXIT, handleExit);
		}
		
		override public function listNotificationInterests():Array
		{
			return [SearchProxy.SEARCH];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case SearchProxy.SEARCH :
					ui.moveThis();
				break;
			}
		}
		
		private function handleExit(event:Event):void
		{
			NativeApplication.nativeApplication.exit();
		}
		
		private function handleGetNext(event:Event):void
		{
			sendNotification(ApplicationFacade.NEXT_SET);
		}
		
		private function handleGetPrev(event:Event):void
		{
			sendNotification(ApplicationFacade.PREV_SET);
		}
		
		private function handleSearch(event:Event):void
		{
			ui.glow.play();
			if(ui.searchText.text.length == 0)
				searchProxy.getRecent();
			else
				searchProxy.search(ui.searchText.text);
		}
		
		private function get ui():SearchBox
		{
			return viewComponent as SearchBox;
		}
	}
}